package controller

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

/**
 * @Author SSWANG
 * @Description //TODO 封装响应
 * @Date 21:19 2022/10/27
 * @Param
 * @return
 **/

type ResponseData struct {
	Code    StatusCode  `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"` // omitempty当data为空时,不展示这个字段
}

// 响应失败, 错误类型Code不固定
func ResponseError(c *gin.Context, statusCode StatusCode) {
	responseData := &ResponseData{
		Code:    statusCode,
		Message: statusCode.Msg(),
		Data:    nil,
	}
	c.JSON(http.StatusOK, responseData)
}

// 响应失败, 错误类型Code不固定, 可输入 错误信息
func ResponseErrorWithMsg(c *gin.Context, statusCode StatusCode, data interface{}) {
	responseData := &ResponseData{
		Code:    statusCode,
		Message: statusCode.Msg(),
		Data:    data,
	}
	c.JSON(http.StatusOK, responseData)
}

// 响应成功, 只有一种Code: CodeSuccess
func ResponseSuccess(c *gin.Context, data interface{}) {
	rd := &ResponseData{
		Code:    CodeSuccess,
		Message: CodeSuccess.Msg(),
		Data:    data,
	}
	c.JSON(http.StatusOK, rd)
}
